(function(window) {

/**
 * @param href
 *            the url contains parameters
 * 
 * @returns the parameters map
 */
function initParameters(href) {
	var parameters = {};
	var idx = href.indexOf('?');
	if (idx > 0) {
		var params = href.substring(idx + 1);
		idx = params.indexOf('#');
		if (idx >= 0) {
			params = params.substring(0, idx);
		}
		var paramsArr = params.split("&");
		for (var i = 0; i < paramsArr.length; ++i) {
			var pstr = paramsArr[i];
			var k, v = null;
			var pidx = pstr.indexOf('=');
			if (pidx > 0) {
				k = pstr.substring(0, pidx);
				v = pstr.substring(pidx + 1);
			} else {
				k = pstr;
			}
			if (parameters[k] != null) {
				if (parameters[k] instanceof Array) {
					parameters[k].push(v);
				} else {
					parameters[k] = [
						parameters[k], v
					];
				}
			} else {
				parameters[k] = v;
			}
		}
	}
	return parameters;
}

// init parameters
var parameters = initParameters(window.location.href);

function getParameter(name) {
	return parameters[name];
}

window["initParameters"] = initParameters;
window["getParameter"] = getParameter;


window["ui_cover"] = function(elm) {
	var e = $(elm);
	if (e[0]) {
		e[0].old_width = e[0].style.width;
		e[0].old_height = e[0].style.height;
		e[0].old_position = e[0].style.position;
	}
	e.addClass('fakewindowcontain')
		.css({
			width:    e.width() + 'px',
			height:   e.height() + 'px',
			position: 'relative'
		});
	var con = $('<div class="ui-widget-overlay"></div>');
	con.appendTo(elm);
	return con;
};

window["ui_uncover"] = function(elm) {
	var e = $(elm);
	e.removeClass('fakewindowcontain')
		.css({
			width:    e[0].old_width,
			height:   e[0].old_height,
			position: e[0].old_position
		});
	$('.ui-widget-overlay', e).remove();
};

window["ui_popup"] = function(elm, content, rect) {
	if (!rect) {
		var e = $(elm);
		rect = {
			top    : 0,
			left   : 0,
			width  : e.width(),
			height : e.height()
		};
	}
	var con = $('<div class="ui-widget ui-widget-content ui-corner-all">'
			//+ '<div class="ui-dialog-content ui-widget-content">'
			+ content
			//+ '</div>'
			+ '</div>');
	con.css({
		top      : rect.top + 'px',
		left     : rect.left + 'px',
		width    : rect.width  + 'px',
		height   : rect.height + 'px',
		position : 'absolute'
	}).appendTo(elm);
	   
	return con;
};

window["cheight"] = function() {
	var scrollHeight,
	offsetHeight;
	// handle IE 6
	if ($.browser.msie && $.browser.version < 7) {
		scrollHeight = Math.max(
			document.documentElement.scrollHeight,
			document.body.scrollHeight
		);
		offsetHeight = Math.max(
			document.documentElement.offsetHeight,
			document.body.offsetHeight
		);
	
		if (scrollHeight < offsetHeight) {
			return $(window).height();
		} else {
			return scrollHeight;
		}
	// handle "good" browsers
	} else {
		return $(document).height();
	}
};

window["cwidth"] = function() {
	var scrollWidth,
		offsetWidth;
	// handle IE 6
	if ($.browser.msie && $.browser.version < 7) {
		scrollWidth = Math.max(
			document.documentElement.scrollWidth,
			document.body.scrollWidth
		);
		offsetWidth = Math.max(
			document.documentElement.offsetWidth,
			document.body.offsetWidth
		);

		if (scrollWidth < offsetWidth) {
			return $(window).width();
		} else {
			return scrollWidth;
		}
	// handle "good" browsers
	} else {
		return $(document).width();
	}
};


window["ajaxDialog"] = function(url, param, title, args, baseurl) {
	param = param || {};
	$.get(url, param, function(data) {
		var content = document.createElement('div');	
	//	$(content).css("background-color","yellow");//李华		
		//$(content).addClass('ui-widget-content');//李华
		content.innerHTML = data;		
		//$(content).draggable();//李华		
		var opts = {'modal': true, 'title': title ? title : '', 'bgiframe': true};
		opts.close = function(event, ui) {
			$(this).remove();
			//alert(baseurl);
			ui_uncover($("body"));
			if (undefined != baseurl)
			{
				if (-1 != baseurl.indexOf("cgi"))
				{
					window.location = encodeURI(baseurl);
				}
			}
		};
		initOpts(opts, args);
		$(content).dialog(opts);			
		//$(content).draggable({handel:"ui-dialog-titlebar"});	//李华	
		$(':text').addClass('ui-widget ui-widget-content ui-corner-all');		
//		$('select', content).combobox();
		$(':submit, button', content).button();
			
		
	});
	function initOpts(opts, args) {
		args = args || {};
		if (opts && args) {
			if (!args.width) {
				args['width'] = 340;
			}
			var left=($(document).width()-args['width'])/2;
			//alert(args.position);
			/*if (!args.position) {
				//args['position'] = [left,"top"];
				args['position'] = [left,100];
			}*/
			for (opt in args) {
				//alert("opt="+opt+"--"+args[opt]);
				opts[opt] = args[opt];
			}
		}
	};
};

window["lockDialog"] = function(url, param, id, title, args) {
	param = param || {};
	$.get(url, param, function(data) {
		var content = document.createElement('div');
		content.setAttribute("id", id);
		content.innerHTML = data;
		var opts = {'modal': true, create: function(event, ui){$('.ui-dialog-titlebar').hide();}};
		opts.close = function(event, ui) {
			$(this).remove();
		};
		$(this).removeClass('.ui-dialog-titlebar');
		initOpts(opts, args);
		$(content).dialog(opts);
		$(':text').addClass('ui-widget ui-widget-content ui-corner-all');
//		$('select', content).combobox();
		$(':submit, button', content).button();
	});
	function initOpts(opts, args) {
		args = args || {};
		if (opts && args) {
			if (!args.width) {
				args['width'] = 340;
			}
			for (opt in args) {
				opts[opt] = args[opt];
			}
		}
	};
};

/*if ($(document).dialog)
window["alert"] = function(msg) {
	$('<div>' + msg + '</div>').dialog({
		modal: true,
		buttons: {
			"Ok": function() { 
				$(this).dialog("close"); 
				$(this).dialog("destroy"); 
			}
		},
		open: function(evt, ui) {
			$(this).siblings('.ui-dialog-buttonpane').css({
				'text-align': 'right'
			});
		}
	});
};*/

window["isPhoneNumber"] = function(value) {
	return /^[\d\-]*$/.test(value);
};

window["isDigit"] = function(value) {
	return /^[\d]*$/.test(value);
};

window["isIPV4"] = function(value) {
	return /^([\d]{1,3}\.){3}([\d]{1,3})$/.test(value);
};


})(window);

function getByTag( name){ return document.getElementsByTagName(name);}
